<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/layouts/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" href="${ctxLib }/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
<title>文章列表</title>
<style type="text/css">  
	.ztree .line{
		    line-height: 0;
		    border-top:none;
    		float: none;
	}
    .ztree li span.button.ico_docu {  
        background-position: -110px 0;  
        margin-right: 2px;  
        vertical-align: top;  
	}  
</style>
</head>
<body class="pos-r">
	<div class="pos-a" style="width:200px;left:0;top:0; bottom:0; height:100%; border-right:1px solid #e5e5e5; background-color:#f5f5f5; overflow:auto;">
		<ul id="treeDemo" class="ztree" style="margin-top: 35px;"></ul>
		<input type="hidden"  id="curId"  >
	</div>
	<div style="margin-left:200px;">
		<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 内容管理 <span class="c-gray en">&gt;</span> 文章管理 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
		<div class="page-container">
			<div class="text-c"> 日期范围：
				<input type="text" onfocus="WdatePicker({ maxDate:'#F{$dp.$D(\'logmax\')||\'%y-%M-%d\'}' })" id="logmin" class="input-text Wdate" style="width:120px;">
				-
				<input type="text" onfocus="WdatePicker({ minDate:'#F{$dp.$D(\'logmin\')}',maxDate:'%y-%M-%d' })" id="logmax" class="input-text Wdate" style="width:120px;">
				<input type="text" name="search_name" id="search_name" placeholder=" 文章标题" style="width:250px" class="input-text">
				<button name="" onclick="searchArticle();" class="btn btn-success" type="submit"><i class="Hui-iconfont">&#xe665;</i> 搜文章</button>
			</div>
			<div class="cl pd-5 bg-1 bk-gray mt-20"> 
				<span class="l">
					<a href="javascript:;" onclick="datadel()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a> 
					<a class="btn btn-primary radius" onclick="pre_add('新增文章','${ctx}/article/add')"  href="javascript:;"><i class="Hui-iconfont">&#xe600;</i> 添加文章</a>
				</span> <span class="r">共有数据：<strong id="totalCount">54</strong> 条</span> </div>
			<div class="mt-20">
				<table class="table table-border table-bordered table-striped"  id="example">
					<thead>
						<tr class="text-c">
							<th width="40"><input name="" type="checkbox" value=""></th>
							<th width="40">ID</th>
							<th width="100">标题</th>
							<th width="40">排序值</th>
							<th width="40">发布时间</th>
							<th width="40">所属栏目</th>
							<th width="60">发布状态</th>
							<th width="60">操作</th>
						</tr>
					</thead>
					<tbody>
					</tbody>
				</table>
			</div>
		</div>
	</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="${ctxLib }/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="${ctxLib }/layer/2.4/layer.js"></script>
<script type="text/javascript" src="${ctxStatic}/h-ui/js/H-ui.min.js"></script> 
<script type="text/javascript" src="${ctxStatic}/h-ui.admin/js/H-ui.admin.js"></script>
<!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本--> 
<script type="text/javascript" src="${ctxLib }/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="${ctxLib }/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" src="${ctxLib }/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="${ctxLib }/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="${ctxLib }/shopUtil.js?v=1.0"></script>
<script type="text/javascript" src="${ctxLib }/datatables/1.10.0/jquery.dataTables.min.js"></script> 
<script type="text/javascript">
	$(function(){
		setZTree();
		query(1);
	});
	
	/*新闻-添加*/
	function pre_add(title,url){
		var checkid = $("#curId").val();
		if(checkid != null){
			if(checkid == 1){
				layer.alert("请在左侧选择一个栏目");
			}else{
				var index = layer.open({
					type: 2,
					title: title,
					content: url+"?cid="+checkid
				});
				layer.full(index);	
			}
		}
	}
	
	/*新闻-编辑*/
	function pre_edit(title,url,id){
		if(id != null && id != 0){
			var index = layer.open({
				type: 2,
				title: title,
				content: url+"?aid="+id
			});
			layer.full(index);
		}
	}
	
	/*新闻-搜索*/
	function searchArticle(){
		query($("#curId").val());
	}
	
	function setZTree(){
		 var setting = {
				 view: {
					fontCss : {"font-family": "微软雅黑", "font-size": "16px"}
				 },
				 data: {
						simpleData: {
							enable: true,
							idKey: "id",
							pIdKey: "pid",
							rootPId: 1
						}
				},
				callback: {
					onClick: function(event, treeId, treeNode){
						var id = treeNode.id;
						$("#curId").val(id);
						query(id);
					}
				}
		};
		 var zNodes = JSON.parse('${tree}');
		 zTreeObj = $.fn.zTree.init($("#treeDemo"), setting,  zNodes);
		 var node = zTreeObj.getNodeByParam("id", 1);
		 zTreeObj.selectNode(node);
		 $("#curId").val("1");
	}
	
	function query(cid){
		if(cid == null || cid == ''){
			return ;
		}
		var url = "${ctx}/article/list";
		$("#example").dataTable({
			"searching" : false,
			"bPaginate" : true,		//开启分页
			"bLengthChange" : 10,	// 每行显示记录数  
	        "iDisplayLength" : 6,		// 每页显示行数
			"bLengthChange":true,//是否显示一个每页长度的选择条
			"aLengthMenu":[[1, 3, 6, -1], [1, 3, 6, "All"]],	//第一个为显示每页条目数的选项，第二个是关于这些选项的解释
			"bAutoWidth": true, 		//是否自适应宽度
			"bProcessing":true,		//是否显示“正在处理”这个提示信息
			"bSort":false,				//是否让各列具有按列排序功能
			"bStateSave":true,			//是否打开客户端状态记录功能
			"bDestroy":true,			//dataTable绑定时，将之前的那个数据对象清除掉，换以新的对象设置
			"serverSide":true,			//开启服务器处理模式
			"sAjaxSource":url,			//请求资源路径
			/*
			 使用ajax，在服务端处理数据
			 sSource:即是"sAjaxSource"
			 aoData:要传递到服务端的参数
			 fnCallback:处理返回数据的回调函数
			 */
			 "fnServerData": function(sSource, aoData, fnCallback){
				 var data = {"aodata" : JSON.stringify(aoData)};
				 data.cid = cid;
				 var name = $("#search_name").val();
				 var startTime = $("#logmin").val();
				 var endTime = $("#logmax").val();
				 if(name != null && name != ""){
					 data.search_name = name;
				 }
				 if(startTime != null && startTime != ""){
					 data.search_startTime = startTime;
				 }
				 if(endTime != null && endTime != ""){
					 data.endTime = endTime;
				 }
				  $.ajax( { 
					  'type' : 'POST', 
					  "url": sSource, 
					  "dataType": "json", 
					  "data": data,
					  "success": function(resp) { 
						  	$("#totalCount").text(resp.iTotalRecords);
					   		fnCallback(resp);
					   } 
				  }); 
			 }, 
			 "columns": [
					        { "data": "id","className":"text-c" },
					        { "data": "id","className":"text-c" },
					        { "data": "title","className":"text-c" },
					        { "data": "sort","className":"text-c" },
					        { "data": "publishDate","className":"text-c" },
					        { "data": "categoryName","className":"text-c" },
					        { "data": "isShow","className":"text-c" },
					        { "data": "id","className":"text-c" }
					    ],
					    "columnDefs": [{
					        targets: 0,
					        render: function(data, type, row, meta) {
					            return '<input type="checkbox" name="checkitem"  id="check'+data+'" value="'+data+'"/>';
					        }
					    },{
					        targets: 2,
					        render: function(data, type, row, meta) {
					        	if(data != null && data !=''){
					        		return data;
					        	}else{
					        		return '';
					        	}
					        }
					    },{
					        targets: 4,
					        render: function(data, type, row, meta) {
					        	if(data ==null){
					        		return "暂未发布";
					        	}
					        	return new Date(data).Format('yyyy-MM-dd hh:mm:ss');
					        }
					    },{
					    	targets: 6,
					    	render:function(data, type, row, meta) {
					    		if(data == true){
					    			return "<span class='label label-success radius'>已发布</span>"
					    		}else{
					    			return "<span class='label label-danger radius'>未发布</span>";
					    		}
					    	}
					    },{
					    	targets: 7,
					        render: function(data, type, row, meta) {
					            return '<a title="编辑" href="javascript:;" onclick="pre_edit(\'文章编辑\',\'${ctx}/article/edit\',\''+data+'\');" class="ml-5" style="text-decoration:none">'+
					            '<i class="Hui-iconfont center-font">&#xe6df;</i></a> <a title="删除" href="javascript:;" onclick="datadel(\''+data+'\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont center-font">&#xe6e2;</i></a>';
					        }
					    }]
		});
	}
	
	
	function datadel(id){
		var ids=new Array();
		if(id != null){
			ids.push(id);
		}else{
			$('input[name="checkitem"]:checked').each(function(){ 
				ids.push($(this).val());
			});
		}
		if(ids.length == 0){
			layer.alert("您还未选择任何文章");
			return;
		}
		layer.confirm('确认要删除这些文章么?',function(index){
			$.ajax({
				type: 'POST',
				url: '${ctx}/article/delete',
				data:{ids:ids},
				dataType: 'json',
				success: function(data){
					if(data.success){
						layer.msg('已删除!',{icon:1,time:1000},function(){
							 var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
							query(treeObj.getSelectedNodes()[0].id);
						});
					}else{
						layer.msg(data.message,{icon:1,time:1000});
					}
				},
				error:function(data) {
					console.log(data.msg);
				},
			});
		});
	}
</script> 
</body>
</html>